﻿using FairyGUI;
using System;
using System.Collections;

/// <summary>
/// 游戏UI
/// </summary>
public class UIGameWindow : UIBaseWindow {

    static UIGameWindow _inst;
    public static UIGameWindow inst { get { return _inst; } }

    ///////////////////////////////////////////////////////////////////////////
    UIGamePlayer[] _players;
    GButton _dissolveRoomBtn;
    GButton _inviteBtn;
	GButton _prepareBtn;
	GButton _cancelPrepareBtn;
	GButton _startGameBtn;
    UIGameRoomInfo _roomInfo;
	UIGamePlayCardIndicator _playCardIndicator;
	GComponent _gameMaskLayer;
	UIGameVoice _voice;

	GButton _leaveBtn;

    ///////////////////////////////////////////////////////////////////////////

    public UIGameRoomInfo roomInfo { get { return _roomInfo; } }
	public UIGamePlayCardIndicator playCardIndicator { get { return _playCardIndicator; } }

    ///////////////////////////////////////////////////////////////////////////

    public UIGameWindow()
    {
        _inst = this;

        // 初始化游戏
        base.Init(UIPackage.CreateObject("游戏界面", "游戏界面").asCom);

        // 房间信息
        _roomInfo = new UIGameRoomInfo(uiComp.GetChild("房间信息").asCom);

		// 出牌指示
		_playCardIndicator = new UIGamePlayCardIndicator (uiComp.GetChild("出牌指示器").asCom);

        // 解散房间按钮
        _dissolveRoomBtn = uiComp.GetChild("解散房间按钮").asButton;
        _dissolveRoomBtn.onClick.Add(()=> {
            DebugInfo.Log("解散房间按钮 被点击");
            GameController.inst.EnqueueMessage(
                    new BaseProceduar.Message(Commands.CMD_CTRL_BUTTON_CLICKED, GameCommands.CMD_DISSOLVE_ROOM)
                    );
        });

        // 邀请好友按钮
        _inviteBtn = uiComp.GetChild("邀请好友按钮").asButton;
        _inviteBtn.onClick.Add(() => {
            DebugInfo.Log("邀请好友按钮 被点击");

			GameController.inst.EnqueueMessage(
				new BaseProceduar.Message(Commands.CMD_CTRL_BUTTON_CLICKED, GameCommands.CMD_INVITE_FRIENDS)
			);
        });

		// 准备游戏按钮
		_prepareBtn = uiComp.GetChild("准备游戏按钮").asButton;
		_prepareBtn.onClick.Add (()=>{
			DebugInfo.Log("准备游戏按钮 被点击");

			SendReady(true);
		});

		// 取消准备按钮
		_cancelPrepareBtn = uiComp.GetChild("取消准备按钮").asButton;
		_cancelPrepareBtn.onClick.Add (()=>{
			DebugInfo.Log("取消准备按钮 被点击");

			SendReady(false);
		});

		// 开始游戏按钮
		_startGameBtn = uiComp.GetChild("开始游戏按钮").asButton;
		_startGameBtn.onClick.Add (()=>{
			DebugInfo.Log("开始游戏按钮 被点击");

			SendReady(true);
		});

		// 发消息按钮
		GButton sendMsgBtn = uiComp.GetChild("发信息按钮").asButton;
		sendMsgBtn.onClick.Add (()=>{
			DebugInfo.Log("发信息按钮 被点击");

			UIGameChat.Popup();
		});

		// 语音按钮
		GButton voiceBtn = uiComp.GetChild("语言按钮").asButton;
		voiceBtn.onTouchBegin.Add (()=>{
			DebugInfo.Log("语言按钮 被按下");

			// 开始录音
			_voice.visible = true;
			_voice.BeginVoice();
		});
		voiceBtn.onTouchEnd.Add (()=>{
			DebugInfo.Log("语言按钮 被松开");

			// 结束录音
			_voice.visible = false;
			_voice.EndVoice(voiceBtn == GRoot.inst.touchTarget);
		});

		// 离开按钮
		_leaveBtn = uiComp.GetChild("离开按钮").asButton;
		_leaveBtn.onClick.Add (()=>{
			DebugInfo.Log("离开按钮 被点击");

			UIGameLeave.Popup();
		});

		// 设置按钮
		GButton settBtn = uiComp.GetChild("设置按钮").asButton;
		settBtn.onClick.Add (()=>{
			DebugInfo.Log("设置按钮 被点击");

			// TODO
			UIGameSetting.Popup();
			//NetIOController.inst.Close();
		});

        // 初始化游戏玩家
        _players = new UIGamePlayer[5];
		_players[0] = new UIGameMainPlayer(uiComp.GetChild("玩家下").asCom);
        _players[1] = new UIGamePlayer(uiComp.GetChild("玩家右").asCom);
        _players[2] = new UIGamePlayer(uiComp.GetChild("玩家右上").asCom);
        _players[3] = new UIGamePlayer(uiComp.GetChild("玩家左上").asCom);
        _players[4] = new UIGamePlayer(uiComp.GetChild("玩家左").asCom);

		// 语音窗口
		_voice = new UIGameVoice(uiComp.GetChild("语音组件").asCom);
		_voice.visible = false;

		// 游戏中
		_gameMaskLayer = uiComp.GetChild ("游戏中").asCom;
    }

	public bool gameMaskLayerVisible {
		set {
			_gameMaskLayer.visible = _gameMaskLayer.enabled = value;
		}
	}

	/// <summary>
	/// 设置本地玩家类型
	/// </summary>
	/// <param name="master">If set to <c>true</c> master.</param>
	public void SetMasterType(bool master) 
	{
		_inviteBtn.visible = _inviteBtn.enabled = true;

		_dissolveRoomBtn.visible = _dissolveRoomBtn.enabled = master;
		_startGameBtn.visible = _startGameBtn.visible = false;

		_prepareBtn.visible = _prepareBtn.enabled = !master;
		_cancelPrepareBtn.visible = _cancelPrepareBtn.enabled = false;

		_playCardIndicator.visible = false;
	}

	/// <summary>
	/// 游戏开始隐藏所有按钮
	/// </summary>
	public void HiddenAllBtns()
	{
		_inviteBtn.visible = _inviteBtn.enabled = false;

		_dissolveRoomBtn.visible = _dissolveRoomBtn.enabled = false;
		_startGameBtn.visible = _startGameBtn.visible = false;

		_prepareBtn.visible = _prepareBtn.enabled = false;
		_cancelPrepareBtn.visible = _cancelPrepareBtn.enabled = false;


		_playCardIndicator.visible = true;
	}

	/// <summary>
	/// 显示准备按钮
	/// </summary>
	/// <param name="show">If set to <c>true</c> show.</param>
	public void ShowPrepareBtn(bool show)
	{
		_prepareBtn.visible = _prepareBtn.enabled = show;
		_cancelPrepareBtn.visible = _cancelPrepareBtn.enabled = !show;
	}

	/// <summary>
	/// 显示开始游戏按钮
	/// </summary>
	public void ShowStartGameBtn(bool show)
	{
		_startGameBtn.visible = _startGameBtn.visible = show;
	}

    /// <summary>
    /// 获取玩家
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public UIGamePlayer GetPlayer(int index)
    {
        int diff = index - GameDataKernel.inst.playerIndex;
        if (diff < 0)
        {
            diff += 5;
        }
        return _players[diff];
    }

    /// <summary>
    /// 获取主玩家
    /// </summary>
    /// <returns></returns>
    public UIGameMainPlayer GetMainPlayer()
    {
        return (UIGameMainPlayer)_players[0];
    }

	public void SetPlayCardPlayerIndex(int index) {
		int diff = index - GameDataKernel.inst.playerIndex;
		if (diff < 0)
		{
			diff += 5;
		}
		_playCardIndicator.SetPlayCardIndex (diff);
	}

    public override void OnExit()
    {
        uiComp.Dispose();
    }

	void SendReady(bool ready) {
		if (ready) {
			if (!GameDataKernel.inst.mainPlayer.ready) {
				Hashtable reqArgs = new Hashtable ();
				reqArgs ["roomId"] = GameDataKernel.inst.roomId;
				reqArgs ["uid"] = DataKernel.inst.userInfo.uid;

				UIRootController.inst.ShowMask (true);
				NetIOController.inst.SendMessage (
					new IOMessage (IOCmds.CMSG_PLAYER_CONTINUE, 
						IOStates.STATE_OK, new HashtableEx (reqArgs)));
			}
		} else {
			if (GameDataKernel.inst.mainPlayer.ready) {
				Hashtable reqArgs = new Hashtable ();
				reqArgs ["roomId"] = GameDataKernel.inst.roomId;
				reqArgs ["uid"] = DataKernel.inst.userInfo.uid;

				UIRootController.inst.ShowMask (true);
				NetIOController.inst.SendMessage (
					new IOMessage (IOCmds.CMSG_PLAYER_CANCEL_READY, 
						IOStates.STATE_OK, new HashtableEx (reqArgs)));
			}
		}
	}
}
